********************************************************************************
***************************descriptives 2012, 2013 & 2014****************************
********************************************************************************

*Globals

global ctr 		AT BE DE DK EL ES FI FR IE IT NL PT SE UK	
global year 	10 11 12 13 14 15 16 17
global macro 	gen con cov wait dur rep sk gdp1 mipex	
global ses		female age hed ue1 ue2 ue3 type1 type2 type3 type4
global micro	ub fob tr1 tr2 tr3 eur tcn $ses	

use "t0", clear


*Standardize sample

qui reg ub fob $ses
gen n=e(sample)


*Recalibrate weights

generate pwx=.

foreach y in $ctr {
	foreach x in $year {
		di "Country: `y' Year: `x'"
		count if year==20`x' & country=="`y'"
		scalar s=r(N)
		count if country=="`y'"
		scalar N=r(N)
		replace pwx=(s/N)*RB050 if year==20`x' & country=="`y'"
	}
}

*Calculate mean values

keep if n==1
drop n

	**Macro-level

	gen sam=.
	replace cov=100*cov
	replace rep=100*rep

	foreach CT in $ctr{
		di "`CT'"
		foreach var in $macro {
			sum `var' if country=="`CT'" 
			replace `var'=r(mean) if country=="`CT'"
		}
		count if country=="`CT'"
		replace sam=r(N) if country=="`CT'"
	}


	**Micro-level
	foreach CT in $ctr{
		di "`CT'"
		foreach var in $micro {
		sum `var' [aw=pwx] if country=="`CT'"
		replace `var'=r(mean) if country=="`CT'"
		}
	}

*Reduce to country-level dataset

bysort country: gen n=_n
keep if n==1

keep country $macro $micro sam
order country $macro $micro sam

*Reshape into final format
	
xpose, clear varname format(%9.2f)
rename _varname Variables
order Variables

*Generate totals
drop if _n==1
egen all= rmean(AT-UK)
replace all=201611 if Variables=="sam"
format all %9.2f

*Rename columns
rename v1 AT
rename v2 BE
rename v3 DE
rename v4 DK
rename v5 EL
rename v6 ES
rename v7 FI
rename v8 FR
rename v9 IE
rename v10 IT
rename v11 NL
rename v12 PT
rename v13 SE
rename v14 UK



*Rename rows
replace Variables="Benefit Generosity Index" if Variables=="gen"
replace Variables="Qualification period (weeks)" if Variables=="con"
replace Variables="Unemployment insurance coverage (%)" if Variables=="cov"
replace Variables="Replacement rate (%)" if Variables=="rep"
replace Variables="Benefit duration (weeks)" if Variables=="dur"
replace Variables="Waiting days" if Variables=="wait"
replace Variables="High-skilled in labour force in %" if Variables=="skill"
replace Variables="GDP growth (t-1) in %" if Variables=="gdp1"
replace Variables="MIPEX" if Variables=="mipex"
replace Variables="Unemployment benefit receipt" if Variables=="ub"
replace Variables="Foreign-born" if Variables=="fob"
replace Variables="< 5 years of residence" if Variables=="tr1"
replace Variables="5 -- 9 years of residence" if Variables=="tr2"
replace Variables="> 9 years of residence" if Variables=="tr3"
replace Variables="Non-European foreign-born" if Variables=="tcn"
replace Variables="European foreign-born" if Variables=="eur"
replace Variables="< 4 months unemployment" if Variables=="ue1"
replace Variables="4 -- 9 months unemployment" if Variables=="ue2"
replace Variables="> 9 months unemployment" if Variables=="ue3"
replace Variables="Female" if Variables=="female"
replace Variables="Age" if Variables=="age"
replace Variables="Tertiary Education" if Variables=="hed"
replace Variables="1 adult no children" if Variables=="type1"
replace Variables="> 1 adult no children" if Variables=="type2"
replace Variables="1 adult + children" if Variables=="type3"
replace Variables="> 1 adult + children" if Variables=="type4"
replace Variables="\textit{Sample size}" if Variables=="sam"

*Save
dataout, save(descriptives) tex  replace dec(2)
